package com.newtouch.bxzs.database.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.newtouch.bxzs.common.base.model.bo.DataDictionaryValueBO;
import com.newtouch.bxzs.common.base.response.BaseResponse;
import com.newtouch.bxzs.common.base.response.RespGenerator;
import com.newtouch.bxzs.common.base.util.RedisUtils;
import com.newtouch.bxzs.database.config.MyLog;
import com.newtouch.bxzs.database.model.bo.DataDictionaryEntryBO;
import com.newtouch.bxzs.database.model.vo.DataDictionaryEntryChangeVO;
import com.newtouch.bxzs.database.model.vo.DataDictionaryEntryListVO;
import com.newtouch.bxzs.database.model.vo.DataDictionaryEntryVO;
import com.newtouch.bxzs.database.service.DataDictionaryEntryService;
import com.newtouch.bxzs.database.service.DataDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据字典项的controller类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021年1月5日</td>
 * <td>zhangqianyang</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author zhangqianyang
 * @version 1.0.0
 * @since 1.0.0
 */
@Api(tags = "数据字典项管理")
@RestController
@SuppressWarnings("unchecked")
@RequestMapping("/dataDictionaryEntryController")
public class DataDictionaryEntryController {
    @Resource
    private DataDictionaryEntryService dataDictionaryEntryService;

    @Resource
    private DataDictionaryService dataDictionaryService;

    @Autowired
    RedisUtils redisUtils;

    /**
     * 静态变量：日志
     */
    private static final Log logger = LogFactory.getLog(DataDictionaryEntryController.class);

    @ApiOperation("获取数据字典项列表")
    @PostMapping("/getDataDictionaryEntryListById")
    @MyLog(systemFunctionName = "数据字典项管理", systemFunctionUrl = "/dataDictionaryEntryController/getDataDictionaryEntryListById", userOperationType = 4, userOperationSummary = "获取数据字典项列表")
    public BaseResponse<IPage<DataDictionaryEntryBO>> getDataDictionaryEntryListById(
            @RequestBody DataDictionaryEntryListVO dataDictionaryEntryListVO) {
        logger.info("通过id获取数据字典项列表");
        return RespGenerator.returnOK(dataDictionaryEntryService.getDataDictionaryEntryListById(dataDictionaryEntryListVO));
    }

    @ApiOperation("通过编码获取数据字典项(可搜索)")
    @GetMapping("/getDataDictionaryEntryListByCode")
    @MyLog(systemFunctionName = "数据字典项管理", systemFunctionUrl = "/dataDictionaryEntryController/getDataDictionaryEntryListByCode", userOperationType = 4, userOperationSummary = "通过编码获取数据字典项")
    public BaseResponse<List<DataDictionaryValueBO>> getDataDictionaryEntryListByCode(
            @ApiParam(value = "字典编码code", required = true) @RequestParam String dictionaryCode,
            @ApiParam(value = "字典项名称") @RequestParam(required = false) String dataDictionaryEntryName) {
        logger.info("通过编码获取数据字典项");
        return RespGenerator.returnOK(dataDictionaryEntryService.getDataDictionaryEntryListByCode(dictionaryCode,dataDictionaryEntryName));
    }

    @ApiOperation("批量逻辑删除数据字典项")
    @PostMapping("/deleteDataDictionaryEntry")
    @MyLog(systemFunctionName = "数据字典项管理", systemFunctionUrl = "/dataDictionaryEntryController/deleteDataDictionaryEntry", userOperationType = 3, userOperationSummary = "批量逻辑删除数据字典项")
    public BaseResponse<String> deleteDataDictionaryEntry(
            @RequestBody @ApiParam(value = "数据字典项id的集合", required = true) List<String> dataDictionaryEntryIds) {
        logger.info("批量逻辑删除数据字典项");
        String dataDictionaryId = dataDictionaryEntryService.deleteDataDictionaryEntry(dataDictionaryEntryIds);
        /* 更新缓存*/
        updateRedis(dataDictionaryId);
        return RespGenerator.returnOK("批量逻辑删除成功");
    }

    @ApiOperation("新增/修改数据字典项")
    @PostMapping("/modifyDataDictionaryEntry")
    @MyLog(systemFunctionName = "数据字典项管理", systemFunctionUrl = "/dataDictionaryEntryController/modifyDataDictionaryEntry", userOperationType = 5, userOperationSummary = "新增/修改数据字典项")
    public BaseResponse<String> modifyDataDictionaryEntry(
            @RequestBody DataDictionaryEntryVO dataDictionaryEntryVO) {
        logger.info("新增或者修改数据字典项");
        String message = dataDictionaryEntryService.modifyDataDictionaryEntry(dataDictionaryEntryVO);
        /* 新增不更新缓存，修改才更新缓存*/
        if (!StringUtils.isEmpty(dataDictionaryEntryVO.getDataDictionaryId())) {
            /* 更新缓存*/
            updateRedis(dataDictionaryEntryVO.getDataDictionaryId());
        }
        return RespGenerator.returnOK(message);
    }

    @ApiOperation("获取数据字典项详细信息")
    @PostMapping("/getDataDictionaryEntryDetail")
    @MyLog(systemFunctionName = "数据字典项管理", systemFunctionUrl = "/dataDictionaryEntryController/getDataDictionaryEntryDetail", userOperationType = 4, userOperationSummary = "获取数据字典项详细信息")
    public BaseResponse<DataDictionaryEntryBO> getDataDictionaryEntryDetail(
            @RequestBody @ApiParam(name = "dataDictionaryEntryId", value = "数据字典项id", required = true) String dataDictionaryEntryId) {
        logger.info("获取数据字典项详细信息");
        return RespGenerator.returnOK(dataDictionaryEntryService.getDataDictionaryEntryDetail(dataDictionaryEntryId));
    }

    @ApiOperation("启用/禁用数据字典项")
    @PostMapping("/changeDataDictionaryEntryStatus")
    @MyLog(systemFunctionName = "数据字典项管理", systemFunctionUrl = "/dataDictionaryEntryController/changeDataDictionaryEntryStatus", userOperationType = 2, userOperationSummary = "启用/禁用数据字典项")
    public BaseResponse<String> changeDataDictionaryEntryStatus(
            @RequestBody DataDictionaryEntryChangeVO dataDictionaryEntryChangeVO) {
        logger.info("启用或者禁用数据字典项");
        String dataDictionaryId = dataDictionaryEntryService.changeDataDictionaryEntryStatus(dataDictionaryEntryChangeVO);
        /* 更新缓存*/
        updateRedis(dataDictionaryId);
        return RespGenerator.returnOK("启用或者禁用数据字典项成功");
    }

    /**
     * 通过数据字典id更新缓存
     *
     * @param dataDictionaryId 数据字典id
     */
    private void updateRedis(String dataDictionaryId) {
        /* 通过id拿到code，因为数据库存的关联字段是id,而且这里需要code，所以要多查一次*/
        String dataDictionaryCode = dataDictionaryService.getDataDictionaryCodeById(dataDictionaryId);
        /* 根据数据字典编码查询出对应数据字典项列表*/
        List<DataDictionaryValueBO> dataDictionaryValueBOList = dataDictionaryService.getDataDictionaryEntry(dataDictionaryCode);
        redisUtils.set(dataDictionaryCode.toUpperCase(), JSON.toJSONString(dataDictionaryValueBOList));
        logger.info("更新缓存成功");
    }
}
